package com.ssy.lingxi.member.merchant.utils;

import com.ssy.lingxi.member.merchant.entity.MemberConfigLabelDO;
import com.ssy.lingxi.member.merchant.model.bo.MemberDetailLabelBO;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员注册资料关联的标签工具类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-05-09
 */
public class MemberConfigLabelUtil {

    /**
     * PaaS修改会员注册资料后，根据新增标签值，修改所有会员注册资料中的标签值
     * @param registerDetailLabels 会员注册资料中的标签值
     * @param configLabels PaaS修改后的注册资料标签值
     */
    public static void removeOrUpdateIdByValue(List<MemberDetailLabelBO> registerDetailLabels, List<MemberConfigLabelDO> configLabels) {
        if(CollectionUtils.isEmpty(registerDetailLabels)) {
            return;
        }

        if(CollectionUtils.isEmpty(configLabels)) {
            registerDetailLabels = new ArrayList<>();
        }

        registerDetailLabels.removeIf(detailLabel -> configLabels.stream().noneMatch(configLabel -> configLabel.getLabelValue().equals(detailLabel.getLabelValue())));

        registerDetailLabels.forEach(detailLabel -> configLabels.stream().filter(configLabel -> configLabel.getLabelValue().equals(detailLabel.getLabelValue())).findFirst().ifPresent(configLabel -> detailLabel.setLabelId(configLabel.getId())));
    }
}
